//
//  UIImageViewExtensions.swift
//  Fangxukanghan
//
//  Created by 常超群的MacBookPro on 2021/3/30.
//

import Foundation
import Kingfisher

public extension UIImageView {
    /// 根据url设置图片，有默认的placehold
    func ccq_setImage(_ imageUrlString: String?, placeholder: UIImage? = nil, loadProgressBlock: ((_ receivedSize: Int64, _ totalSize: Int64) -> Void)? = nil, loadSuccessClosure: ((UIImage) -> Void)? = nil) {
        guard let imageUrlString = imageUrlString else {
            self.image = placeholder
            return
        }
        let escapedString = imageUrlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
        
        kf.setImage(with: URL.init(string: escapedString), placeholder: placeholder, options: nil, progressBlock: loadProgressBlock) { (result) in
            switch result {
                case .success(let rst):
                    loadSuccessClosure?(rst.image)
                case .failure(let error):
                    print("图片加载失败: \(error.localizedDescription)")
            }
        }
    }
}
